package zuo.algo.util;

import zuo.algo.data.graph.Edge;
import zuo.algo.data.graph.Graph;
import zuo.algo.data.graph.Node;

public class GraphUtils {

	public static Graph createGraph(int[][] matrix) {
		Graph graph = new Graph();
		for (int[] integers : matrix) {
			int weight = integers[0];
			Integer from = integers[1];
			Integer to = integers[2];
			if (!graph.nodes.containsKey(from)) {
				graph.nodes.put(from, new Node(from));
			}
			if (!graph.nodes.containsKey(to)) {
				graph.nodes.put(to, new Node(to));
			}
			Node fromNode = graph.nodes.get(from);
			Node toNode = graph.nodes.get(to);
			Edge newEdge = new Edge(weight, fromNode, toNode);
			fromNode.nexts.add(toNode);
			fromNode.out++;
			toNode.in++;
			fromNode.edges.add(newEdge);
			graph.edges.add(newEdge);
		}
		return graph;
	}

	public static Graph createGraph1(int[][] matrix) {
		Graph graph = new Graph();
		for (int i = 0; i < matrix.length; i++) {
			graph.nodes.put(i, new Node(i));
		}
		for (int i = 0; i < matrix.length; i++) {
			Node sourceNode = graph.nodes.get(i);
			for (int j = 0; j < matrix[i].length; j++) {
				Node targetNode = graph.nodes.get(j);
				if (matrix[i][j] > 0) {
					targetNode.in = targetNode.in + 1;
					sourceNode.out = sourceNode.out + 1;
					sourceNode.nexts.add(targetNode);
					Edge edge = new Edge(matrix[i][j], sourceNode, targetNode);
					sourceNode.edges.add(edge);
					graph.edges.add(edge);
				}
			}
		}
		return graph;
	}

}
